package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.UserCityServerCentre;
import com.sc.nft.entity.dto.GetUserCityServerCentreListDTO;
import com.sc.nft.entity.dto.GetUserCityServerCentrePageDTO;
import com.sc.nft.entity.vo.CityServerCentreExportVO;
import com.sc.nft.entity.vo.GetUserCityServerCentreListVO;
import com.sc.nft.entity.vo.GetUserCityServerCentrePageVO;
import com.sc.nft.entity.vo.UserWorkRoomExcludeWhiteListRankVO;
import com.sc.nft.mapper.UserCityServerCentreMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Slf4j
@Repository
public class UserCityServerCentreDao extends ServiceImpl<UserCityServerCentreMapper, UserCityServerCentre> {

    /**
     * 新增用户城市服务中心
     *
     * @return
     */
    public UserCityServerCentre selectByUserId(Long userId) {
        QueryWrapper<UserCityServerCentre> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        UserCityServerCentre userCityServerCentre = baseMapper.selectOne(queryWrapper);
        return userCityServerCentre;
    }

    public List<UserCityServerCentre> selectByUserIdList(List<Long> userIdList) {
        QueryWrapper<UserCityServerCentre> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList);
        List<UserCityServerCentre> userCityServerCentreList = baseMapper.selectList(queryWrapper);
        return userCityServerCentreList;
    }


    /**
     * 根据userId修改用户省市区
     *
     * @param userCityServerCentre
     * @return
     */
    public Boolean byUserIdSaveModel(UserCityServerCentre userCityServerCentre) {
        return baseMapper.byUserIdSaveModel(userCityServerCentre);
    }

    public List<GetUserCityServerCentreListVO> getUserCityServerCentreList(GetUserCityServerCentreListDTO getUserCityServerCentreListDTO) {
        return baseMapper.getUserCityServerCentreList(getUserCityServerCentreListDTO);
    }


    /**
     * 获取最近的城市服务中心用户id
     *
     * @param userId
     * @return
     */
    public Long accessTheNearestCityServiceCenter(Long userId) {
        return baseMapper.accessTheNearestCityServiceCenter(userId);
    }

    public Page<GetUserCityServerCentrePageVO> selectPage(GetUserCityServerCentrePageDTO getUserCityServerCentreListDTO) {
        return baseMapper.selectPage(getUserCityServerCentreListDTO.toMp(), getUserCityServerCentreListDTO);
    }

    /**
     * 获取团队的城市服务中心数量
     *
     * @param userId
     * @return
     */
    public Integer teamCityServiceCenter(Long userId) {
        return baseMapper.teamCityServiceCenter(userId);
    }

    public List<Long> getJuniorUserIds(Long participationSmeltUserId, List<Long> participationSmeltUserIds) {
        return baseMapper.getJuniorUserIds(participationSmeltUserId, participationSmeltUserIds);
    }

    /**
     * 获取下级服务中心列表
     *
     * @param userId
     * @return
     */

    public List<Long> getNextServiceCity(Long userId) {
        return baseMapper.getNextServiceCity(userId);
    }
    public List<CityServerCentreExportVO> centerServerExport() {
        return baseMapper.centerServerExport();
    }
    /**
     * 获取城市服务中心排除白名单的列表
     *
     * @return
     */
    public List<UserWorkRoomExcludeWhiteListRankVO> getUserCityServerCentreExcludeWhiteListRank() {
        return baseMapper.getUserCityServerCentreExcludeWhiteListRank();
    }
    /**
     * 获取城市服务中心总数
     *
     * @return
     */
    public Integer getCount() {
        LambdaQueryWrapper<UserCityServerCentre> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        return baseMapper.selectCount(lambdaQueryWrapper);
    }

    /**
     * 根据用户id获取城市服务中心
     * @param userId
     * @return
     */
    public UserCityServerCentre getByUserId(Long userId) {
        LambdaQueryWrapper<UserCityServerCentre> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserCityServerCentre::getUserId, userId);
        wrapper.last("limit 1");
        return baseMapper.selectOne(wrapper);
    }

    public Integer getUserCityServerExcludeExchequerList(Long userId, Date startTime, Date endTime) {
        return baseMapper.getUserCityServerExcludeExchequerList(userId,startTime,endTime);
    }

    /**
     * 获取用户所在的所有城市服务中心 DaoId
     *
     * @param userId
     * @return
     */
    public List<Long> getServiceCenterUserIdListByUserId(Long userId){
        return baseMapper.getServiceCenterUserIdListByUserId(userId);
    }


    /**
     * 获取用户所在的所有品牌Dao DaoId
     *
     * @param userId
     * @return
     */
    public List<Long> getDaoUserIdListByUserId(Long userId){
        return baseMapper.getDaoUserIdListByUserId(userId);
    }

}
